<?php
class MSEP_Http extends MSEP_Object
{


      private $_method;



      private $_curl;



      public $_fgc;



      public $_fsockopen;



      public $headers;



      public function __init(MSEP $core)
      {
            $this->core = $core;

            $this->_curl = false;

            $this->_fgc = false;

            $this->_fsockopen = false;

            $defined_functions = get_defined_functions();

            $methods = array("curl", "fsockopen", "fgc");

            if (in_array("curl_init", $defined_functions["internal"]) && is_callable("curl_init"))
            {
                  $this->_curl = true;
            }

            if (ini_get("allow_url_fopen") == 1 && in_array("file_get_contents", $defined_functions["internal"]) && is_callable("file_get_contents") && in_array("get_headers", $defined_functions["internal"]) && is_callable("get_headers"))
            {
                  $this->_fgc = true;
            }

            if (in_array("fsockopen", $defined_functions["internal"]) && is_callable("fsockopen"))
            {
                  $this->_fsockopen = true;
            }

            if (empty($this->_method))
            {
                  foreach ($methods as $method)
                  {
                        if ($this->{"_{$method}"})
                        {
                              $this->_method = $method;

                              break;
                        }
                  }
            }
      }



      public function http($url, $type = "headers", $post = "")
      {
            if ($this->_method == "curl" && $this->http_curl($url, $type, $post))
            {
                  return true;
            }
            elseif ($this->_method == "fsockopen" && $this->http_fsockopen($url, $type, $post))
            {
                  return true;
            }
            elseif ($this->_method == "fgc" && $this->http_fgc($url, $type))
            {
                  return true;
            }

            return false;
      }



      private function http_curl($url, $type = "headers", $post = "")
      {
            $ch = curl_init();

            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_AUTOREFERER, true);
            curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; fi; rv:1.9.0.14) Gecko/2009082707 Firefox/3.0.14");
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
            curl_setopt($ch, CURLOPT_TIMEOUT, 5);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
            curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
            curl_setopt($ch, CURLOPT_HEADER, false);

            if (!empty($post))
            {
                  curl_setopt($ch, CURLOPT_POST, 1);
                  curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
            }

            if ($type == "headers")
            {
                  curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
                  curl_setopt($ch, CURLOPT_NOBODY, true);
            }
            elseif ($type == "contents")
            {
                  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            }

            if (($result = curl_exec($ch)) === false)
            {
                  curl_close($ch);

                  return false;
            }

            if ($type == "headers")
            {
                  $this->headers = curl_getinfo($ch);

                  if (empty($this->headers["http_code"]) || $this->headers["http_code"] == 0)
                  {
                        curl_close($ch);

                        return false;
                  }

                  curl_close($ch);

                  return true;
            }
            elseif ($type == "contents")
            {
                  $this->contents = $result;

                  if (empty($this->contents))
                  {
                        curl_close($ch);

                        return false;
                  }

                  curl_close($ch);

                  return true;
            }

            curl_close($ch);

            return false;
      }



      private function http_fgc($url, $type = "headers", $post = "")
      {
            if ($type == "headers")
            {
                  if (($response = get_headers($url, 1)) !== false)
                  {
                        $this->headers = $response;
                        $this->headers["url"] = $url;

                        $value = explode(" ", $this->headers[0]);
                        $this->headers["http_code"] = $value[1];

                        array_shift($this->headers);

                        if (empty($this->headers["http_code"]) || $this->headers["http_code"] == 0)
                        {
                              return false;
                        }

                        return true;
                  }
            }
            elseif ($type == "contents")
            {
                  $opts = array("http" => array("method" => "POST",
                                                 "max_redirects" => 10,
                                                 "timeout" => 5,
                                                 "user_agent" => "Mozilla/5.0 (Windows; U; Windows NT 6.0; fi; rv:1.9.0.14) Gecko/2009082707 Firefox/3.0.14"));

                  if (!empty($post))
                  {
                        $opts['http']['header'] = "Content-type: application/x-www-form-urlencoded\r\n";
                        $opts['http']['content'] = $post;
                  }

                  $context = stream_context_create($opts);

                  if (($response = file_get_contents($url, false, $context)) !== false)
                  {
                        $this->contents = $response;

                        if (empty($this->contents))
                        {
                              return false;
                        }

                        return true;
                  }
            }

            return false;
      }



      private function http_fsockopen($url, $type = "headers", $post = "")
      {
            $parsedUrl = parse_url($url);
            $host = $parsedUrl["host"];

            if (isset($parsedUrl["path"]))
            {
                  $path = $parsedUrl["path"];
            }
            else
            {
                  $path = "/";
            }

            if (isset($parsedUrl["query"]))
            {
                  $path .= "?".$parsedUrl["query"];
            }

            if (isset($parsedUrl["port"]))
            {
                  $port = $parsedUrl["port"];
            }
            else
            {
                  $port = "80";
            }

            $response = "";

            $fp = @fsockopen($host, "80", $errno, $errstr, 5);

            if ($fp !== false)
            {
                  $data = "GET {$path} HTTP/1.0\r\n".
                          "Host: {$host}\r\n".
                          "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; fi; rv:1.9.0.14) Gecko/2009082707 Firefox/3.0.14\r\n".
                          "Referer: http://{$host}\r\n\r\n";

                  if (!empty($post))
                  {
                        $data .= $post;
                  }

                  fwrite($fp, $data);

                  stream_set_timeout($fp, 5);

                  while ($line = fread($fp, 4096))
                  {
                        $response .= $line;
                  }

                  fclose($fp);

                  if ($type == "headers")
                  {
                        $response = substr($response, 0, strpos($response, "\r\n\r\n"));
                        $response = explode("\r\n", $response);

                        $this->headers = array();
                        $this->headers["url"] = $url;

                        foreach ($response as $value)
                        {
                              if (is_int(strpos($value, "HTTP")))
                              {
                                    $value = explode(" ", $value);
                                    $this->headers["http_code"] = $value[1];
                              }
                              else
                              {
                                    $value = explode(": ", $value);
                                    $this->headers[$value[0]] = $value[1];
                              }
                        }

                        if (empty($this->headers["http_code"]) || $this->headers["http_code"] == 0)
                        {
                              return false;
                        }

                        return true;
                  }
                  elseif ($type == "contents")
                  {
                        $pos = strpos($response, "\r\n\r\n");
                        $response = substr($response, $pos+4);

                        $this->contents = $response;

                        if (empty($this->contents))
                        {
                              return false;
                        }

                        return true;
                  }
            }

            return false;
      }


}
?>